<template>
  <div class="brand-setting">
    <div class="top">
      <p>品牌设置</p>
    </div>
    <div class="bottom">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="name" label="品牌名称:">
          <el-input
            v-model="form.name"
            size="medium"
            maxlength="20"
            show-word-limit
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item prop="information" label="门店信息:（至少填写一个商场）" style="white-space: nowrap">
          <div class="shop-wrapper">
            <div class="row">
              <div class="title">全球家居1号店</div>
              <div class="info">
                <ul>
                  <li>
                    <span>门店地址:</span>
                    <el-input
                      v-model="form.information[0].shop_address"
                      size="medium"
                      style="width: 200px"
                    />
                  </li>
                  <li>
                    <span>联系电话:</span>
                    <el-input v-model="form.information[0].tel" size="medium" style="width: 200px" />
                  </li>
                  <li>
                    <span>核销口令:</span>
                    <el-input
                      v-model="form.information[0].code"
                      size="medium"
                      style="width: 200px"
                    />
                  </li>
                </ul>
              </div>
            </div>
            <div class="row">
              <div class="title">欧丽洛雅吴中路商场</div>
              <div class="info">
                <ul>
                  <li>
                    <span>门店地址:</span>
                    <el-input
                      v-model="form.information[1].shop_address"
                      size="medium"
                      style="width: 200px"
                    />
                  </li>
                  <li>
                    <span>联系电话:</span>
                    <el-input v-model="form.information[1].tel" size="medium" style="width: 200px" />
                  </li>
                  <li>
                    <span>核销口令:</span>
                    <el-input
                      v-model="form.information[1].code"
                      size="medium"
                      style="width: 200px"
                    />
                  </li>
                </ul>
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item prop="cover" label="封面图:">
          <el-upload
            action="http://ddsc.izhixiu.cn/api/common/upload"
            :file-list="coverFileList"
            list-type="picture-card"
            :on-preview="handleCoverPreview"
            :on-remove="handleCoverRemove"
            :on-success="hanldeCoverSuccess"
            name="photo"
            :limit="1"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <p style="margin-left:65px;font-size:12px;color:#7B7B7B;">建议尺寸：750*804px</p>
          <el-dialog :visible.sync="coverDialogVisible">
            <img width="100%" :src="coverDialogImageUrl" alt />
          </el-dialog>
        </el-form-item>
        <el-form-item prop="banners" label="轮播图:">
          <el-upload
            action="http://ddsc.izhixiu.cn/api/common/upload"
            name="photo"
            :file-list="carouselFileList"
            list-type="picture-card"
            :on-preview="handleCarouselPreview"
            :on-remove="handleCarouselRemove"
            :on-success="hanldeCarouselSuccess"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <p style="margin-left:65px;font-size:12px;color:#7B7B7B;">尺寸：750px*804px,最多上传5张轮播图</p>
          <el-dialog :visible.sync="carouselDialogVisible">
            <img width="100%" :src="carouselDialogImageUrl" alt />
          </el-dialog>
        </el-form-item>
        <el-form-item prop="description" label="简介:">
          <el-input
            v-model="form.description"
            maxlength="42"
            show-word-limit
            type="textarea"
            size="medium"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item prop="detail" label="详情:">
          <div style="width: 1000px; margin-left: 45px">
            <Tinymce ref="tinymce" v-model="form.detail" />
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="save">
      <button class="submit" @click="submit">保存</button>
    </div>
  </div>
</template>
<script>
import Tinymce from "@/components/Tinymce";
import { createBrandItem } from "@/api/http/brand";
const ERR_OK = 200;
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      coverFileList: [],
      carouselFileList: [],
      form: {
        name: "",
        information: [
          {
            shop_id: "1",
            shop_address: "",
            tel: "",
            code: ""
          },
          {
            shop_id: "2",
            shop_address: "",
            tel: "",
            code: ""
          }
        ],
        cover: "",
        banners: [],
        description: "",
        detail: ""
      },
      coverDialogVisible: false,
      coverDialogImageUrl: "",
      carouselDialogVisible: false,
      carouselDialogImageUrl: "",
      rules: {
        name: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
        information: [
          {
            required: true,
            message: "请输入门店信息",
            trigger: "blur"
          }
        ],
        cover: [
          {
            required: true,
            message: "请输入封面图",
            trigger: "blur"
          }
        ],
        banners: [
          {
            required: true,
            message: "请上传轮播图",
            trigger: "blur"
          }
        ],
        description: [
          {
            required: true,
            message: "请输入简介",
            trigger: "blur"
          }
        ],
        detail: [
          {
            required: true,
            message: "请输入详情",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    if (this.$route.query.detail) {
      const brandDetail = this.$route.query.detail;
      this.form.name = brandDetail.name;
      brandDetail.information.forEach(item => {
        this.form.information.forEach(fItem => {
          if (parseInt(fItem.shop_id) === parseInt(item.shop_id)) {
            fItem.shop_address = item.shop_address;
            fItem.tel = item.tel;
            fItem.code = item.code;
          }
        });
      });
      this.form.cover = brandDetail.cover;
      this.coverFileList.push({
        name: brandDetail.cover.split("com/")[1],
        url: brandDetail.cover
      });
      this.form.banners = brandDetail.banners;
      if (brandDetail.banners instanceof Array) {
        brandDetail.banners.forEach(item => {
          this.carouselFileList.push({
            name: item.split("com/")[1],
            url: item
          });
        });
      }

      this.form.description = brandDetail.description;
      this.form.detail = brandDetail.detail;
    }
  },
  methods: {
    handleCoverPreview(file) {
      this.coverDialogImageUrl = file.url;
      this.coverDialogVisible = true;
    },
    handleCoverRemove(file, fileList) {
      this.coverDialogImageUrl = "";
      this.form.cover = "";
    },
    hanldeCoverSuccess(response, file, fileList) {
      if (response.code === ERR_OK) {
        this.form.cover = response.data.photo;
      }
    },
    handleCarouselPreview(file) {
      this.carouselDialogImageUrl = file.url;
      this.carouselDialogVisible = true;
    },
    handleCarouselRemove(file) {
      const fIndex = this.form.banners.findIndex(item => {
        return item === file.url;
      });
      if (fIndex >= 0) {
        this.form.banners.splice(fIndex, 1);
      }
      this.carouselDialogImageUrl = "";
    },
    hanldeCarouselSuccess(response, file, fileList) {
      if (response.code === ERR_OK) {
        this.form.banners.push(response.data.photo);
      }
    },
    submit() {
      if (this.$route.query.detail) {
        this.form.id = this.$route.query.detail.id;
      }
      this.$refs["form"].validate(valid => {
        console.log(this.form, "----this.form");

        console.log(this.form.information, "---information");
        if (this.form.information) {
          var item = this.form.information.filter(res => {
            return res.shop_address != "";
          });
          this.form.information = item;
        }

        if (valid) {
          this._createBrandItem(this.form).then(res => {
            if (res.code === ERR_OK) {
              this.$router.push("/brand/index");
            }
          });
        } else {
          return false;
        }
      });
    },
    _createBrandItem({
      name,
      information,
      cover,
      banners,
      description,
      detail,
      id
    }) {
      return createBrandItem({
        name,
        information,
        cover,
        banners,
        description,
        detail,
        id
      })
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res);
            this.$message({
              message: "操作成功",
              type: "success",
              showClose: true
            });
            return Promise.resolve(res);
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>
<style lang="scss" scoped>
@import "@/styles/el-button.scss";
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  p {
    line-height: 44px;
    color: rgba(68, 68, 68, 1);
    font-size: 18px;
    text-align: left;
    font-family: Roboto;
    font-weight: bold;
  }
}
.bottom {
  .shop-wrapper {
    margin-top: 40px;
    margin-left: 10px;
    .row {
      display: flex;
      align-items: center;
      margin-top: 10px;
      .title {
        margin-right: 10px;
      }
      ul {
        display: flex;
        align-items: center;
        margin: 0;
        padding: 0;
        li {
          list-style: none;
          margin-right: 10px;
          span {
            line-height: 23px;
            color: rgba(102, 102, 102, 1);
            font-size: 14px;
            text-align: center;
            font-family: Roboto;
          }
        }
      }
    }
  }
}
.save {
  .submit {
    display: block;
    margin: 0 auto;
    width: 200px;
    line-height: 48px;
    border-radius: 6px;
    background-color: rgba(43, 51, 62, 1);
    text-align: center;
    font-size: 16px;
    color: #fff;
    border: none;
    outline: none;
  }
}
</style>
